#pragma once
#include <string>
#include <map>
#include <thread>
#include <mutex>
class ServiceHandler;
struct event_base;
struct bufferevent;
class ServiceTimer;
class ServiceTimerHandler;
class Service
{
public:
	Service();
	bool start();
	void stop();
	event_base* getBase();
	void sendCommand(std::string cmd, void* data);
	void setHandler(ServiceHandler* handler);
	ServiceTimer* createTimer(int timeout, ServiceTimerHandler* timerHandler, bool repid = false);
private:
	bool procCommand(int type, void* data);
private:
	static void onChannelRecv(bufferevent* bev, void* data);
	static void* doWork(void* data);
private:
	event_base* m_base;
	int m_sockets[2];
	bufferevent* m_bevRecv;
	bool m_running;
	ServiceHandler* m_handler;
	std::thread* m_thread;
	std::mutex m_mutex;
};
